package org.example.leetCode;

/**
 * @Author Qinkai
 * @Description TODO $
 * @Date $ 2021-09-24$
 * @Param $
 * @return $
 **/
public class Flatten {
    public Node flatten(Node head) {
        if (head == null) {
            return null;
        }
        Node temp = head;
        while (temp != null) {
            if (temp.child != null) {
                Node next = temp.next;
                Node child = flatten(temp.child);
                temp.next = child;
                child.prev = temp;
                temp.child = null;
                if (next != null) {
                    while (temp.next != null) {
                        temp = temp.next;
                    }
                    temp.next = next;
                    next.prev = temp;
                }
            }
            temp = temp.next;
        }
        return head;
    }

}


class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node child;
}
